package com.industrial.operation.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.gxhzb.ChartsParams;
import com.industrial.operation.domain.gxhzb.OperationIndexManager;
import com.industrial.operation.domain.gxhzb.OperationKfqInfo;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;

import com.industrial.operation.service.IOperationKfqInfoService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工业运行评价-开发区信息Controller
 *
 * @author SangGuangCan
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/operation/kfqkh/manage")
public class OperationKfqInfoController extends BaseController
{
    @Autowired
    private IOperationKfqInfoService operationKfqInfoService;

    /**
     * 查询工业运行评价-开发区信息列表
     */
    @RequiresPermissions("operation:operation:list")
    @GetMapping("/list")
    public TableDataInfo list(OperationKfqInfo operationKfqInfo)
    {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Set<String> roles = loginUser.getRoles();

//        if(sysUser.isAdmin()){
//            if(StringUtils.isNotBlank(operationKfqInfo.getCity())){
//                operationKfqInfo.setCity(operationKfqInfo.getCity());
//            }
//        }else{
//            if(ArrayUtil.contains(roles.toArray(),"x_01_011")){
//                operationKfqInfo.setCounty(sysUser.getDistrict());
//            } else if (ArrayUtil.contains(roles.toArray(), "s_01_011")) {
//                if(StringUtils.isBlank(operationKfqInfo.getCity())){
//                    operationKfqInfo.setCity(sysUser.getCity());
//                }else{
//                    if(StringUtils.endsWith(operationKfqInfo.getCity(),"0000")){
//                        operationKfqInfo.setProvince(operationKfqInfo.getCity());
//                        operationKfqInfo.setCity(null);
//                    }else if(StringUtils.endsWith(operationKfqInfo.getCity(),"00")){
//                        operationKfqInfo.setCity(operationKfqInfo.getCity());
//                    }else{
//                        operationKfqInfo.setCounty(operationKfqInfo.getCity());
//                        operationKfqInfo.setCity(null);
//                    }
//                }
//
//            }else if (ArrayUtil.contains(roles.toArray(), "p_01_011")){
//                if(StringUtils.isBlank(operationKfqInfo.getCity())){
//                    operationKfqInfo.setProvince(sysUser.getProvince());
//                }else{
//                    if(StringUtils.endsWith(operationKfqInfo.getCity(),"0000")){
//                        operationKfqInfo.setProvince(operationKfqInfo.getCity());
//                        operationKfqInfo.setCity(null);
//                    }else if(StringUtils.endsWith(operationKfqInfo.getCity(),"00")){
//                        operationKfqInfo.setCity(operationKfqInfo.getCity());
//                    }else{
//                        operationKfqInfo.setCounty(operationKfqInfo.getCity());
//                        operationKfqInfo.setCity(null);
//                    }
//                }
//            }
//        }

        if(ArrayUtil.contains(roles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(operationKfqInfo.getCity()) || StringUtils.endsWith(operationKfqInfo.getCity(),"0000")){
                operationKfqInfo.setCity(null);
            }
        }else if(ArrayUtil.contains(roles.toArray(),"s_01_011") || ArrayUtil.contains(roles.toArray(),"Sixteen_Zones")){
            if(!StringUtils.equals(operationKfqInfo.getCity(),sysUser.getCity())){
                operationKfqInfo.setCity(sysUser.getCity());
            }
        }else if(ArrayUtil.contains(roles.toArray(), "kfq")){
            operationKfqInfo.setDzn(sysUser.getRemark());
        }


        startPage();
        List<OperationKfqInfo> list = operationKfqInfoService.selectOperationKfqInfoList(operationKfqInfo);
        return getDataTable(list);
    }

    /**
     * 导出工业运行评价-开发区信息列表
     */
    @RequiresPermissions("operation:operation:export")
    @Log(title = "工业运行评价-开发区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationKfqInfo operationKfqInfo)
    {
        List<OperationKfqInfo> list = operationKfqInfoService.selectOperationKfqInfoList(operationKfqInfo);
        ExcelUtil<OperationKfqInfo> util = new ExcelUtil<OperationKfqInfo>(OperationKfqInfo.class);
        util.exportExcel(response, list, "工业运行评价-开发区信息数据");
    }

    /**
     * 获取工业运行评价-开发区信息详细信息
     */
    @RequiresPermissions("operation:operation:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(operationKfqInfoService.selectOperationKfqInfoByBaseId(baseId));
    }

    /**
     * 新增工业运行评价-开发区信息
     */
    @RequiresPermissions("operation:operation:add")
    @Log(title = "工业运行评价-开发区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperationKfqInfo operationKfqInfo)
    {
        return toAjax(operationKfqInfoService.insertOperationKfqInfo(operationKfqInfo));
    }

    /**
     * 修改工业运行评价-开发区信息
     */
    @RequiresPermissions("operation:operation:edit")
    @Log(title = "工业运行评价-开发区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperationKfqInfo operationKfqInfo)
    {
        return toAjax(operationKfqInfoService.updateOperationKfqInfo(operationKfqInfo));
    }

    /**
     * 删除工业运行评价-开发区信息
     */
    @RequiresPermissions("operation:operation:remove")
    @Log(title = "工业运行评价-开发区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(operationKfqInfoService.deleteOperationKfqInfoByBaseIds(baseIds));
    }

    /**
     * 查询表的标头信息
     * @param params 查询参数
     * @return
     */
    @GetMapping("/queryTableHeader")
    public AjaxResult queryTableHeader(OperationIndexManager params){
        if(StringUtils.isBlank(params.getYears())){
            return AjaxResult.warn("没有找到年份");
        }
        List<Map<String,String>> result = operationKfqInfoService.queryTableHeader(params);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     *     下载导入数据的模版文件
     * </p>
     * @param params 查询参数
     * @param resp 响应
     */
    @PostMapping("/downloadImportExcelTemplate")
    public void downloadImportExcelTemplate(@RequestBody OperationIndexManager params,HttpServletResponse resp){
        String fileName = "开发区导入数据模版";
        Workbook wb = operationKfqInfoService.createTemplateWb(params);

        try(OutputStream os = resp.getOutputStream();){
            resp.setHeader("content-type", "application/octet-stream");
            resp.setContentType("application/octet-stream");
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            if(wb != null){
                wb.write(os);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * <p>
     *     导入分数
     * </p>
     * @param file
     * @param years
     * @return
     */
    @PostMapping("/importExcelTemplateScore")
    public AjaxResult importExcelTemplateScore(@RequestPart("file") MultipartFile file,@RequestParam("years") String years){
        operationKfqInfoService.importExcelTemplateScore(file,years);
        return AjaxResult.success();
    }

    /**
     * <p>
     *     导入值
     * </p>
     * @param file
     * @param years
     * @return
     */
    @PostMapping("/importExcelTemplateValue")
    public AjaxResult importExcelTemplateValue(@RequestPart("file") MultipartFile file,@RequestParam("years") String years){
        operationKfqInfoService.importExcelTemplateValue(file,years);
        return AjaxResult.success();
    }

    @GetMapping("/queryCharts")
    public AjaxResult queryCharts(ChartsParams chartsParams){
        return operationKfqInfoService.queryCharts(chartsParams);
    }

    @GetMapping("/loadKfpData")
    public AjaxResult loadKfpData(){
        return operationKfqInfoService.loadKfpData();
    }

    /**
     * 导出开发区数据
     * @param resp
     */
    @PostMapping("/exportExcelData")
    public void exportExcelData(@RequestBody OperationKfqInfo params,HttpServletResponse resp){
        String fileName = "开发区数据";
        Workbook wb = operationKfqInfoService.createExportDataWb(params);

        try(OutputStream os = resp.getOutputStream();){
            resp.setHeader("content-type", "application/octet-stream");
            resp.setContentType("application/octet-stream");
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            if(wb != null){
                wb.write(os);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
